+function($){
  var shopcount = [],shopnamecount=[],k=0;
  var brandcount = [],brandnamecount=[],j=0;
  var emcount = [],emnamecount=[],m=0;
  var dialogWidth = '800px';
  window.shopcount = shopcount;
  window.shopnamecount = shopnamecount;
  window.brandcount = brandcount;
  window.brandnamecount = brandnamecount;
  window.emcount = emcount;
  window.emnamecount = emnamecount;
  window.j=j;
  window.k=k;
  window.m=m;
  function shopiteminit(){
    if(window.shopcount != shopcount && window.shopnamecount != shopnamecount){
      shopcount=[];
      shopnamecount=[];
      window.shopcount=shopcount;
      window.shopnamecount=shopnamecount;
    }
      for (var i = 0; i < shopcount.length; i++) {
        if(shopcount[i]){
          str = '<a class="J_shopitemR btn btn-xs btn-danger" data-shopid="'+shopcount[i]+'">'+shopnamecount[i]+'<i class="icon-remove pickbox-icon"></i></a>';
          // str = '<a class="J_shopitemR btn btn-xs btn-info" data-shopid="'+shopcount[i]+'">'+shopnamecount[i]+'</a>';
          $('#shopcount').append(str);
        }
      }
  }
  function branditeminit(){
    if(window.brandcount != brandcount && window.brandnamecount != brandnamecount){
      brandcount=[];
      brandnamecount=[];
      window.brandcount=brandcount;
      window.brandnamecount=brandnamecount;
    }
      for (var i = 0; i < brandcount.length; i++) {
        if(brandcount[i]){
          str = '<a class="J_branditemR btn btn-xs btn-danger" data-bid="'+brandcount[i]+'">'+brandnamecount[i]+'<i class="icon-remove pickbox-icon"></i></a>';
          $('#brandcount').append(str);
        }
      }
  }
  function emiteminit(){
    if(window.emcount != emcount && window.emnamecount != emnamecount){
      emcount=[];
      emnamecount=[];
      window.emcount=emcount;
      window.emnamecount=emnamecount;
    }
      for (var i = 0; i < emcount.length; i++) {
        if(emcount[i]){
          str = '<a class="J_emitemR btn btn-xs btn-info" data-emid="'+emcount[i]+'">'+emnamecount[i]+'</a>';
          $('#emcount').append(str);
        }
      }
  }
  function changeshopitem(){
    var count = 0;
    for (var i = 0; i < shopcount.length; i++) {
      if(shopcount[i]){
        count++;
      }
    }
    var str = '已选'+count+'个店铺';
    if(count==0){
      str = '未选择店铺';
    }
    $('.selectshop').text('编辑');
    if(shopnamecount.length==0){
      $('.selectshop').text('选择店铺');
    }
    $('.J_selectshopitem').text(str);
  }
  function changebranditem(){
    var count = 0;
    for (var i = 0; i < brandnamecount.length; i++) {
      if(brandnamecount[i]){
        count++;
      }
    }
    var str = '已选'+count+'个品牌';
    if(count==0){
      str = '未选择品牌';
    }
    $('.selectbrand').text('编辑');
    if(brandnamecount.length==0){
      $('.selectbrand').text('选择品牌');
    }
    $('.J_selectbranditem').text(str);
  }
  function changeemitem(){
    var count = 0;
    for (var i = 0; i < emnamecount.length; i++) {
      if(emnamecount[i]){
        count++;
      }
    }
    var str = '已选'+count+'个员工';
    if(count==0){
      str = '未选择员工';
    }
    $('.selectem').text('编辑');
    if(emnamecount.length==0){
      $('.selectem').text('选择员工');
    }
    $('.J_selectemitem').text(str);
  }
  function ispick(arr,item){
    var flag = false;
    for (var i = 0; i < arr.length; i++) {
      if(arr[i]==item){
        flag = true;
        break;
      }
    }
    return flag;
  }

  $('body').on('mouseover', '.btn-picked', function() {
    $(this).children('i').removeClass('icon-ok').addClass('icon-minus');
  });
  $('body').on('mouseleave ', '.btn-picked', function() {
    $(this).children('i').removeClass('icon-minus').addClass('icon-ok');
  });
  //brandselect
  // $('body').on('click', '.J_bpage', function() {
  //    var url = $(this).data('uri');
  //   $.get(url, function(data) {
  //     var json = JSON.parse(data);
  //     var next = parseInt(json.page)+1;
  //     var prev = parseInt(json.page)-1;
  //     if(json.page==json.total && json.page==1){
  //       var strpage='';
  //     }else if(json.page==1){
  //       var strpage = '<span style="cursor:pointer" class="J_bpage" data-uri="/brand/getallbrand?per_page='+next+'">下一页</span>';
  //     }else if(json.page==json.total){
  //       var strpage = '<span style="cursor:pointer" class="J_bpage" data-uri="/brand/getallbrand?per_page='+prev+'">上一页</span>';
  //     }else{
  //       var strpage = '<span style="cursor:pointer" class="J_bpage" data-uri="/brand/getallbrand?per_page='+prev+'">上一页</span>&nbsp;<span style="cursor:pointer" class="J_bpage" data-uri="/brand/getallbrand?per_page='+next+'">下一页</span>';
  //     }
  //     $('#J_page').html(strpage);
  //     $('#J_brandlist').html('');
  //     var cont = '';
  //     for (var i = 0; i < json.brandlist.length; i++) {
  //       if(ispick(window.brandcount,json.brandlist[i].bid)){
  //         cont = cont+'<a class="J_branditem btn btn-xs btn-success disabled" data-bid="'+json.brandlist[i].bid+'">'+json.brandlist[i].bname+'<i class="icon-ok pickbox-icon"></i></a>';
  //       }else{
  //         cont = cont+'<a class="J_branditem btn btn-xs btn-info" data-bid="'+json.brandlist[i].bid+'">'+json.brandlist[i].bname+'<i class="icon-plus pickbox-icon"></i></a>';
  //       }
  //     }
  //     $('#J_brandlist').html(cont);
  //   });
  // });
  $('body').on('click', '.selectbrand', function() {
    $.get('/brand/getallbrand', function(data) {
      var json = JSON.parse(data);
      var next = parseInt(json.page)+1;
      var prev = parseInt(json.page)-1;
      if(json.page==json.total && json.page==1){
        var strpage='';
      }else if(json.page==1){
        var strpage = '<span style="cursor:pointer" class="J_bpage" data-uri="/brand/getallbrand?per_page='+next+'">下一页</span>';
      }else if(json.page==json.total){
        var strpage = '<span style="cursor:pointer" class="J_bpage" data-uri="/brand/getallbrand?per_page='+prev+'">上一页</span>';
      }else{
        var strpage = '<span style="cursor:pointer" class="J_bpage" data-uri="/brand/getallbrand?per_page='+prev+'">上一页</span>&nbsp;<span style="cursor:pointer" class="J_bpage" data-uri="/brand/getallbrand?per_page='+next+'">下一页</span>';
      }
      var cont = '';
      for (var i = 0; i < json.brandlist.length; i++) {
        if(ispick(window.brandcount,json.brandlist[i].bid)){
          cont = cont+'<a class="btn btn-xs btn-picked J_brandpicked" data-bid="'+json.brandlist[i].bid+'">'+json.brandlist[i].bname+'<i class="icon-ok pickbox-icon"></i></a>';
        }else{
          cont = cont+'<a class="J_branditem btn btn-xs btn-info" data-bid="'+json.brandlist[i].bid+'">'+json.brandlist[i].bname+'<i class="icon-plus pickbox-icon"></i></a>';
        }
      }
      var content = ''+
                    '<div class="pickbox"><h4 class="pickbox-title label label-lg label-info arrowed-right">可选品牌</h4>'+
                    // '<div class="pickbox-select"><input class="search-input input-sm" type="text" placeholder="输入关键字"><button class="btn btn-xs">搜索</button></div>'+
                    '<div id="J_brandlist" class="pickbox-item">'+cont+'</div>'+
                    '<h4 class="selectbox-title label label-lg label-success arrowed-right">已选品牌</h4>'+
                    '<div id="brandcount" class="picked-box"></div>';
                    '<div id="J_page">'+strpage+'</div>';
                    // '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="addbranditem">确定</button></div>';
      deletedialog = dialog({
        title:'选择品牌',
        content: content,
        width:dialogWidth,
        skin:'  dialog-box',
        lock:true,
        padding:'10px'
      }).showModal(branditeminit());
    });
  });
  $('body').on('click', '.J_branditem', function() {
    $(this).removeClass('btn-info J_branditem').addClass('btn-picked J_brandpicked');
    $(this).children('i').removeClass('icon-plus').addClass('icon-minus');
    // icon-minus
    var str = '<a class="J_branditemR btn btn-xs btn-danger" data-bid="'+$(this).data('bid')+'">'+$(this).text()+'<i class="icon-remove pickbox-icon"></i></a>';
    $('#brandcount').append(str);
    for (var i = 0; i < brandcount.length; i++) {
      if(brandcount[i]==$(this).data('bid')){
        return false;
      }
    }
    brandcount[window.j]=$(this).data('bid');
    brandnamecount[window.j]=$(this).text();
    window.j++;
    changebranditem();
  });
  $('body').on('click', '.J_branditemR', function() {
    if(brandcount.length==1){
      brandcount=[];
      brandnamecount=[];
      window.brandcount=brandcount;
      window.brandnamecount=brandnamecount;
    }
    for (var i = 0; i < brandcount.length; i++) {
      if(brandcount[i]==$(this).data('bid')){
        brandcount.splice(i,1);
        brandnamecount.splice(i,1);
        break;
      }
    }
    var fuck = $('#J_brandlist').children('.J_brandpicked[data-bid="'+$(this).data('bid')+'"]');
    fuck.removeClass('btn-picked btn-success J_brandpicked').addClass('btn-info J_branditem');
    fuck.children('i').removeClass('icon-ok').addClass('icon-plus');
    $(this).remove();
    changebranditem();
  });
  $('body').on('click', '.J_brandpicked', function() {
    var fuck = $('#brandcount').children('.J_branditemR[data-bid="'+$(this).data('bid')+'"]');
    fuck.remove();
    $(this).removeClass('btn-picked J_brandpicked').addClass('btn-info J_branditem');
    $(this).children('i').removeClass('icon-minus').addClass('icon-plus');
    if(brandcount.length==1){
      brandcount=[];
      brandnamecount=[];
      window.brandcount=brandcount;
      window.brandnamecount=brandnamecount;
    }
    for (var i = 0; i < brandcount.length; i++) {
      if(brandcount[i]==$(this).data('bid')){
        brandcount.splice(i,1);
        brandnamecount.splice(i,1);
        break;
      }
    }
    // $(this).removeClass('btn-info J_branditem').addClass('btn-picked J_brandpicked');
    // $(this).children('i').removeClass('icon-plus').addClass('icon-ok');
    changebranditem();
  });

  //emselect
  // $('body').on('click', '.J_empage', function() {
  //    var url = $(this).data('uri');
  //   $.get(url, function(data) {
  //     var json = JSON.parse(data);
  //     var next = parseInt(json.page)+1;
  //     var prev = parseInt(json.page)-1;
  //     if(json.page==json.total && json.page==1){
  //       var strpage='';
  //     }else if(json.page==1){
  //       var strpage = '<span style="cursor:pointer" class="J_empage" data-uri="/employee/getallemployee?per_page='+next+'">下一页</span>';
  //     }else if(json.page==json.total){
  //       var strpage = '<span style="cursor:pointer" class="J_empage" data-uri="/employee/getallemployee?per_page='+prev+'">上一页</span>';
  //     }else{
  //       var strpage = '<span style="cursor:pointer" class="J_empage" data-uri="/employee/getallemployee?per_page='+prev+'">上一页</span>&nbsp;<span style="cursor:pointer" class="J_bpage" data-uri="/employee/getallemployee?per_page='+next+'">下一页</span>';
  //     }
  //     $('#J_page').html(strpage);
  //     $('#J_emlist').html('');
  //     var cont = '';
  //     for (var i = 0; i < json.emlist.length; i++) {
  //       cont = cont+'<a class="J_emitem btn btn-xs btn-info" data-emid="'+json.emlist[i].emid+'">'+json.emlist[i].emname+'</a>';
  //     }
  //     $('#J_emlist').html(cont);
  //   });
  // });
  $('body').on('click', '.selectem', function() {
    $.get('/employee/getallemployee', function(data) {
      var json = JSON.parse(data);
      var next = parseInt(json.page)+1;
      var prev = parseInt(json.page)-1;
      if(json.page==json.total && json.page==1){
        var strpage='';
      }else if(json.page==1){
        var strpage = '<span style="cursor:pointer" class="J_empage" data-uri="/employee/getallemployee?per_page='+next+'">下一页</span>';
      }else if(json.page==json.total){
        var strpage = '<span style="cursor:pointer" class="J_empage" data-uri="/employee/getallemployee?per_page='+prev+'">上一页</span>';
      }else{
        var strpage = '<span style="cursor:pointer" class="J_empage" data-uri="/employee/getallemployee?per_page='+prev+'">上一页</span>&nbsp;<span style="cursor:pointer" class="J_bpage" data-uri="/employee/getallemployee?per_page='+next+'">下一页</span>';
      }
      var cont = '';
      for (var i = 0; i < json.emlist.length; i++) {
        if(ispick(window.emcount,json.emlist[i].emid)){
          cont = cont+'<a class="btn btn-xs btn-picked J_empicked" data-emid="'+json.emlist[i].emid+'">'+json.emlist[i].emname+'<i class="icon-ok pickbox-icon"></i></a>';
        }else{
          cont = cont+'<a class="J_emitem btn btn-xs btn-info" data-emid="'+json.emlist[i].emid+'">'+json.emlist[i].emname+'<i class="icon-plus pickbox-icon"></i></a>';
        }
      }
      var content = ''+
                    '<div class="pickbox"><h4 class="pickbox-title label label-lg label-info arrowed-right">可选员工</h4>'+
                    // '<div class="pickbox-select"><input class="search-input input-sm" type="text" placeholder="输入关键字"><button class="btn btn-xs">搜索</button></div>'+
                    '<div id="J_emlist" class="pickbox-item">'+cont+'</div>'+
                    '<h4 class="selectbox-title label label-lg label-success arrowed-right">已选员工</h4>'+
                    '<div id="emcount" class="picked-box"></div>';
                    // '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="addbranditem">确定</button></div>';
      deletedialog = dialog({
        title:'选择员工',
        content: content,
        width:dialogWidth,
        skin:'  dialog-box',
        lock:true,
        padding:'10px'
      }).showModal(emiteminit());
    });
  });
  $('body').on('click', '.J_emitem', function() {
    $(this).removeClass('btn-info J_emitem').addClass('btn-picked J_empicked');
    $(this).children('i').removeClass('icon-plus').addClass('icon-minus');
    var str = '<a class="J_emitemR btn btn-xs btn-danger" data-emid="'+$(this).data('emid')+'">'+$(this).text()+'<i class="icon-remove pickbox-icon"></i></a>';
    $('#emcount').append(str);
    for (var i = 0; i < emcount.length; i++) {
      if(emcount[i]==$(this).data('emid')){
        return false;
      }
    }
    emcount[window.m]=$(this).data('emid');
    emnamecount[window.m]=$(this).text();
    window.m++;
    changeemitem();
  });
  $('body').on('click', '.J_emitemR', function() {
    if(emcount.length==1){
      emcount=[];
      emnamecount=[];
      window.emcount=emcount;
      window.emnamecount=emnamecount;
    }
    for (var i = 0; i < emcount.length; i++) {
      if(emcount[i]==$(this).data('emid')){
        emcount.splice(i,1);
        emnamecount.splice(i,1);
        break;
      }
    }
    var fuck = $('#J_emlist').children('.J_empicked[data-emid="'+$(this).data('emid')+'"]');
    fuck.removeClass('btn-picked btn-success J_empicked').addClass('btn-info J_emitem');
    fuck.children('i').removeClass('icon-ok').addClass('icon-plus');
    $(this).remove();
    changeemitem();
  });
  $('body').on('click', '.J_empicked', function() {
    var fuck = $('#emcount').children('.J_emitemR[data-emid="'+$(this).data('emid')+'"]');
    fuck.remove();
    $(this).removeClass('btn-picked J_empicked').addClass('btn-info J_emitem');
    $(this).children('i').removeClass('icon-minus').addClass('icon-plus');
    if(emcount.length==1){
      emcount=[];
      emnamecount=[];
      window.emcount=emcount;
      window.emnamecount=emnamecount;
    }
    for (var i = 0; i < emcount.length; i++) {
      if(emcount[i]==$(this).data('emid')){
        emcount.splice(i,1);
        emnamecount.splice(i,1);
        break;
      }
    }
    changebranditem();
  });

  //shopselect
  // $('body').on('click', '.J_spage', function() {
  //    var url = $(this).data('uri');
  //   $.get(url, function(data) {
  //     var json = JSON.parse(data);
  //     var next = parseInt(json.page)+1;
  //     var prev = parseInt(json.page)-1;
  //     if(json.page==json.total && json.page==1){
  //       var strpage='';
  //     }else if(json.page==1){
  //       var strpage = '<span style="cursor:pointer" class="J_spage" data-uri="/shop/getallshop?per_page='+next+'">下一页</span>';
  //     }else if(json.page==json.total){
  //       var strpage = '<span style="cursor:pointer" class="J_spage" data-uri="/shop/getallshop?per_page='+prev+'">上一页</span>';
  //     }else{
  //       var strpage = '<span style="cursor:pointer" class="J_spage" data-uri="/shop/getallshop?per_page='+prev+'">上一页</span>&nbsp;<span style="cursor:pointer" class="J_spage" data-uri="/shop/getallshop?per_page='+next+'">下一页</span>';
  //     }
  //     $('#J_page').html(strpage);
  //     $('#J_shoplist').html('');
  //     var cont = '';
  //     for (var i = 0; i < json.shoplist.length; i++) {
  //       cont = cont+'<a class="J_shopitem btn btn-xs btn-info" data-shopid="'+json.shoplist[i].shopid+'">'+json.shoplist[i].shopname+'</a>';
  //     }
  //     $('#J_shoplist').html(cont);
  //   });
  // });
  $('body').on('click', '.selectshop', function() {
    $.get('/shop/getallshop', function(data) {
      var json = JSON.parse(data);
      var str = '';
      var next = parseInt(json.page)+1;
      var prev = parseInt(json.page)-1;
      if(json.page==json.total && json.page==1){
        var strpage='';
      }else if(json.page==1){
        var strpage = '<span style="cursor:pointer" class="J_spage" data-uri="/shop/getallshop?per_page='+next+'">下一页</span>';
      }else if(json.page==json.total){
        var strpage = '<span style="cursor:pointer" class="J_spage" data-uri="/shop/getallshop?per_page='+prev+'">上一页</span>';
      }else{
        var strpage = '<span style="cursor:pointer" class="J_spage" data-uri="/shop/getallshop?per_page='+prev+'">上一页</span>&nbsp;<span style="cursor:pointer" class="J_spage" data-uri="/shop/getallshop?per_page='+next+'">下一页</span>';
      }
      var cont = '';

      // for (var i = 0; i < json.emlist.length; i++) {
      //   if(ispick(window.emcount,json.emlist[i].emid)){
      //     cont = cont+'<a class="btn btn-xs btn-picked J_empicked" data-emid="'+json.emlist[i].emid+'">'+json.emlist[i].emname+'<i class="icon-ok pickbox-icon"></i></a>';
      //   }else{
      //     cont = cont+'<a class="J_emitem btn btn-xs btn-info" data-emid="'+json.emlist[i].emid+'">'+json.emlist[i].emname+'<i class="icon-plus pickbox-icon"></i></a>';
      //   }
      // }
      for (var i = 0; i < json.shoplist.length; i++) {
        if(ispick(window.shopcount,json.shoplist[i].shopid)){
          cont = cont+'<a class="btn btn-xs btn-picked J_shoppicked" data-shopid="'+json.shoplist[i].shopid+'">'+json.shoplist[i].shopname+'<i class="icon-ok pickbox-icon"></i></a>';
        }else{
          cont = cont+'<a class="J_shopitem btn btn-xs btn-info" data-shopid="'+json.shoplist[i].shopid+'">'+json.shoplist[i].shopname+'<i class="icon-plus pickbox-icon"></i></a>';
        }
      }
      var content = ''+
                    '<div class="pickbox"><h4 class="pickbox-title label label-lg label-info arrowed-right">可选店铺</h4>'+
                    '<div id="J_shoplist" class="pickbox-item">'+cont+'</div>'+
                    '<h4 class="selectbox-title label label-lg label-success arrowed-right">已选店铺</h4>'+
                    '<div id="shopcount" class="picked-box"></div>';
      deletedialog = dialog({
        title:'选择店铺',
        content: content,
        width:dialogWidth,
        height:'300px',
        skin:'  dialog-box',
        lock:true,
        padding:'10px'
      }).showModal(shopiteminit());
    });
  });
  $('body').on('click', '.J_shopitem', function() {
    $(this).removeClass('btn-info J_shopitem').addClass('btn-picked J_shoppicked');
    $(this).children('i').removeClass('icon-plus').addClass('icon-minus');
    var str = '<a class="J_shopitemR btn btn-xs btn-danger" data-shopid="'+$(this).data('shopid')+'">'+$(this).text()+'<i class="icon-remove pickbox-icon"></i></a>';
    $('#shopcount').append(str);
    for (var i = 0; i < shopcount.length; i++) {
      if(shopcount[i]==$(this).data('shopid')){
        return false;
      }
    }
    shopcount[window.k]=$(this).data('shopid');
    shopnamecount[window.k]=$(this).text();
    window.k++;
    changeshopitem();
  });
  $('body').on('click', '.J_shopitemR', function() {
    if(shopcount.length==1){
      shopcount=[];
      shopnamecount=[];
      window.shopcount = shopcount;
      window.shopnamecount = shopnamecount;
    }
    for (var i = 0; i < shopcount.length; i++) {
      if(shopcount[i]==$(this).data('shopid')){
        shopcount.splice(i,1);
        shopnamecount.splice(i,1);
        break;
      }
    }
    var fuck = $('#J_shoplist').children('.J_shoppicked[data-shopid="'+$(this).data('shopid')+'"]');
    fuck.removeClass('btn-picked btn-success J_shoppicked').addClass('btn-info J_shopitem');
    fuck.children('i').removeClass('icon-ok').addClass('icon-plus');
    $(this).remove();
    changeshopitem();
  });
  $('body').on('click', '.J_shoppicked', function() {
    var fuck = $('#shopcount').children('.J_shopitemR[data-shopid="'+$(this).data('shopid')+'"]');
    fuck.remove();
    $(this).removeClass('btn-picked J_shoppicked').addClass('btn-info J_shopitem');
    $(this).children('i').removeClass('icon-minus').addClass('icon-plus');
    if(shopcount.length==1){
      shopcount=[];
      shopnamecount=[];
      window.shopcount = shopcount;
      window.shopnamecount = shopnamecount;
    }
    for (var i = 0; i < shopcount.length; i++) {
      if(shopcount[i]==$(this).data('shopid')){
        shopcount.splice(i,1);
        shopnamecount.splice(i,1);
        break;
      }
    }
    changeshopitem();
  });

}(jQuery);
